package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.service.MenuService;
import com.itheima.service.UserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @创建人 红莲
 * @创建时间 2020/8/31 8:46
 * @版本 1.0
 * @描述
 */
@RestController
@RequestMapping("/menu")
public class MenuController {
    @Reference
    private MenuService menuService;

    @Reference
    private UserService userService;

    @RequestMapping("/findAll")
    public Result findAll() {
        try {
            List<Menu> all = menuService.findAll();
            return new Result(true, MessageConstant.QUERY_MENU_SUCCESS, all);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_MENU_FAIL);
        }
    }
    @RequestMapping("/{id}/menus")
    public Result findMenuIds(@RequestBody Integer id) {
        try {
            Integer[] menuIds = menuService.findMenuIds(id);
            return new Result(true, MessageConstant.QUERY_MENU_SUCCESS, menuIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_MENU_FAIL);
        }
    }
    @RequestMapping("/findById")
    public Result findById(@RequestParam Integer id){
        try {
            Menu menu = menuService.findById(id);
            return  new Result(true,MessageConstant.QUERY_MENU_SUCCESS,menu);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_MENU_FAIL);
        }
    }
    @RequestMapping("/findMenus")
    public Result findMenus() {
        try {
            UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            String username = userDetails.getUsername();
            List<Menu> menus = (List<Menu>) userService.findByUsername(username);
            return new Result(true, MessageConstant.QUERY_MENU_SUCCESS, menus);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_MENU_FAIL);
        }
    }

    @RequestMapping("/findParent")
    public Result findParent() {
        try {
            List<Menu> parents = menuService.findParent();
            return new Result(true, MessageConstant.QUERY_MENU_SUCCESS, parents);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_MENU_FAIL);
        }
    }

    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        return menuService.pageQuery(queryPageBean);
    }

    @RequestMapping("/add")
    @PreAuthorize("hasAnyAuthority('MENU_ADD')")
    public Result add(@RequestBody Menu menu){
        try {
            menuService.add(menu);
            return  new Result(true,MessageConstant.ADD_MENU_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_MENU_FAIL);
        }
    }
    @RequestMapping("/findChildren/{id}")
    public Result findChildren(@RequestBody Integer id){
        try {
            List<Menu> children = menuService.findChildren(id);
            return  new Result(true,MessageConstant.QUERY_MENU_SUCCESS,children);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_MENU_FAIL);
        }
    }

    @RequestMapping("/update")
    public Result update(@RequestBody Menu menu){
        try {
            menuService.update(menu);
            return new Result(true,MessageConstant.EDIT_MENU_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_MENU_FAIL);
        }
    }
    @RequestMapping("/delete")
    @PreAuthorize("hasAnyAuthority('MENU_DELETE')")//权限校验
    public Result delete(Integer id){
        try {
            menuService.delete(id);
        } catch (RuntimeException e) {
            return new Result(false,e.getMessage());
        } catch (Exception e) {
            return new Result(false, MessageConstant.DELETE_MENU_FAIL);
        }
        return new Result(true,MessageConstant.DELETE_MENU_SUCCESS);
    }
}
